package com.lz.springboot.apollo.control;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.lz.springboot.apollo.Application;
import com.lz.springboot.apollo.config.ApolloConfigApplication;
import com.lz.springboot.apollo.config.ApolloConfigJdbc;
import com.lz.springboot.apollo.entity.CardOpenDoorLog;
import com.lz.springboot.apollo.entity.Community;
import com.lz.springboot.apollo.entity.WeixinOpenId;
import com.lz.springboot.apollo.mongo.dao.CardOpenDoorLogDao;
import com.lz.springboot.apollo.rabbit.DirectSender;
import com.lz.springboot.apollo.rabbit.FanoutSender;
import com.lz.springboot.apollo.rabbit.SimpleSender;
import com.lz.springboot.apollo.service.WeixinOpenIdService;
import com.lz.springboot.log4j2.ApolloConfigLog4j2;
import com.lz.springboot.mongo.ApolloConfigMongo;
import com.lz.springboot.redis.ApolloConfigRedis;
import com.lz.springboot.redis.RedisUtils;

@RestController  
public class IndexController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
	private static final Gson GSON = new Gson();
	
	@Autowired
	private ApolloConfigApplication applicationConfig;
	@Autowired
	private ApolloConfigLog4j2 apolloConfigLog4j2;
	@Autowired
	private ApolloConfigRedis apolloConfigRedis;
	@Autowired
	private ApolloConfigJdbc apolloConfigJdbc;
	@Autowired
	private ApolloConfigMongo apolloConfigMongo;
	@Autowired
	private WeixinOpenIdService weixinOpenIdService;
	@Autowired
	private RedisUtils redisUtils;
    @Autowired
    private CardOpenDoorLogDao cardOpenDoorLogDao;
    @Autowired
    private SimpleSender simpleSender;
    @Autowired
    private FanoutSender fanoutSender;
    @Autowired
    private DirectSender directSender;
    
    
	
    @RequestMapping("/index")  
    public String index() {  
        return applicationConfig.toString();
    } 
    
    @RequestMapping("/redis")  
    public String redis() {  
        return apolloConfigRedis.toString();
    } 
    
    @RequestMapping("/jdbc")  
    public String jdbc() {  
    	return apolloConfigJdbc.toString();
    } 
    
    @RequestMapping("/log4j")  
    public String log4j() {  
    	LOGGER.debug("===========================debug===========================");
    	LOGGER.info("===========================info===========================");
    	LOGGER.error("===========================error===========================");
        return apolloConfigLog4j2.toString();
    } 
    
    @RequestMapping("/weixin")  
    public String weixin() {  
    	Map<String, Object> queryMap = new HashMap<String, Object>();
    	queryMap.put("mobile_phone", "13616052510");
    	WeixinOpenId weixinOpenId = weixinOpenIdService.selectOneByMap(queryMap);
    	return weixinOpenId.toString();
    } 
    
    @RequestMapping("/redisGet")  
    public String redisGet() {
    	String object = redisUtils.get("CommunityCache_1");
    	Community community = GSON.fromJson(object, Community.class); 
        return String.valueOf(community);
    } 
    
    @RequestMapping("/redisSetAndGet")
    public String redisSet() {
    	Community community = new Community();
    	community.setId(1L);
    	community.setName("小区1");
    	boolean setResult = redisUtils.set("CommunityCache_1", GSON.toJson(community));
    	LOGGER.info("setResult={}", setResult);
    	String object = redisUtils.get("CommunityCache_1");
    	Community communityGet = GSON.fromJson(object, Community.class); 
    	return String.valueOf(communityGet);
    } 
    
    @RequestMapping("/mongo")  
    public String mongo() {  
    	return apolloConfigMongo.toString();
    } 
    
    @RequestMapping("/mongoSave")  
    public String mongoSave() {  
    	CardOpenDoorLog cardOpenDoorLog = new CardOpenDoorLog();
    	for (int i = 1; i <= 25; i++) {
        	cardOpenDoorLog.setId(Long.valueOf(i));
        	cardOpenDoorLog.setCard_num("" + i);
        	cardOpenDoorLog.setOpen_time(new Date());
    		cardOpenDoorLogDao.insert(cardOpenDoorLog);
		}
    	return cardOpenDoorLog.toString();
    } 
    
    @RequestMapping("/mongoUpdate")  
    public int mongoUpdate() { 
    	Map<String, Object> queryParams = new HashMap<>();
    	queryParams.put("id", 1L);
		Map<String, Object> updateParams = new HashMap<>();
		updateParams.put("card_num", "111");
		updateParams.put("city", "厦门");
    	return cardOpenDoorLogDao.update(queryParams, updateParams);
    }
    
    @RequestMapping("/mongoUpsert")  
    public int mongoUpsert() { 
    	Map<String, Object> queryParams = new HashMap<>();
    	queryParams.put("id", 1L);
		Map<String, Object> updateParams = new HashMap<>();
		updateParams.put("card_num", "1111");
		updateParams.put("city", "厦门");
    	return cardOpenDoorLogDao.upsert(queryParams, updateParams);
    }
    
    @RequestMapping("/mongoCount")  
    public long mongoCount() {  
    	Map<String, Object> queryParams = new HashMap<>();
    	queryParams.put("id", 1L);
    	return cardOpenDoorLogDao.count(queryParams);
    } 
    
    @RequestMapping("/mongoFindOne")  
    public CardOpenDoorLog mongoFindOne() {  
    	Map<String, Object> queryParams = new HashMap<>();
    	queryParams.put("id", 1L);
    	return cardOpenDoorLogDao.findOne(queryParams);
    } 
    
    @RequestMapping("/mongoFindList")  
    public List<CardOpenDoorLog> mongoFindList() {  
    	Map<String, Object> queryParams = new HashMap<>();
    	return cardOpenDoorLogDao.findList(queryParams, 0, 10);
    } 
    
    @RequestMapping("/rabbitSimpleSender")  
    public String simpleSender() {
    	for (int i = 0; i < 1; i++) {
    		simpleSender.send(String.valueOf(new Date()));
    		try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
    	return "";
    } 
    
    @RequestMapping("/rabbitFanoutSender")  
    public String fanoutSender() {  
    	String msgString="fanoutSender :hello i am likejian";
    	fanoutSender.send(msgString);
    	return "";
    } 
    
    @RequestMapping("/rabbitDirectSender")  
    public String directSender() { 
    	for (int i = 0; i < 1000000; i++) {
	    	String msgString = "directSender :hello i am a";
	    	String routingKey = "a";
	    	directSender.send(msgString, routingKey);
	    	
	    	msgString = "directSender :hello i am b";
	    	routingKey = "b";
	    	directSender.send(msgString, routingKey);
    	}
    	return "";
    } 
}   